﻿

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.Model;
using Gift.DBUtility;//请先添加引用

namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:tbBill
    /// </summary>
    public partial class DALBill
    {
        public DALBill()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbBill");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,50)			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public decimal GetBillMoneyFromCompanyId(string sCompanyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  isnull( sum( isnull(Money,0)),0) as Money  ");
            strSql.AppendFormat(" FROM tbBill where CompanyId={0} ", sCompanyId);

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            else
            {
                return 0;
            }
        }

        public decimal GetBillMoneyYesFromCompanyId(string sCompanyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  isnull( sum( isnull(Money,0)),0) as Money  ");
            strSql.AppendFormat(" FROM tbBill where States='已开票' and CompanyId={0} ", sCompanyId);

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            else
            {
                return 0;
            }
        }

        public decimal GetBillMoneyNoFromCompanyId(string sCompanyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  isnull( sum( isnull(Money,0)),0) as Money  ");
            strSql.AppendFormat(" FROM tbBill where States='未开票' and CompanyId={0} ", sCompanyId);

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            else
            {
                return 0;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gift.Model.tbBill model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbBill(");
            strSql.Append("Id,TitleName,BillTypeId,Money,States,CompanyId,Remark,ClassName,Telephone,ProcessRemark,AdminId,AddBillTime,ProcessBillTime,TaxId,AddressTel,BankCode,ParentId)");
            strSql.Append(" values (");
            strSql.Append("@Id,@TitleName,@BillTypeId,@Money,@States,@CompanyId,@Remark,@ClassName,@Telephone,@ProcessRemark,@AdminId,@AddBillTime,@ProcessBillTime,@TaxId,@AddressTel,@BankCode,@ParentId)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,50),
					new SqlParameter("@TitleName", SqlDbType.NVarChar,200),
					new SqlParameter("@BillTypeId", SqlDbType.Int,4),
					new SqlParameter("@Money", SqlDbType.Float,8),
					new SqlParameter("@States", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@ClassName", SqlDbType.NVarChar,50),
                    new SqlParameter("@Telephone", SqlDbType.NVarChar,50),
                    new SqlParameter("@ProcessRemark", SqlDbType.NVarChar,500),
                    new SqlParameter("@AdminId", SqlDbType.Int,4),
                    new SqlParameter("@AddBillTime", SqlDbType.DateTime),
                    new SqlParameter("@ProcessBillTime", SqlDbType.DateTime),
                    new SqlParameter("@TaxId", SqlDbType.NVarChar,200),
                    new SqlParameter("@AddressTel", SqlDbType.NVarChar,200),
                    new SqlParameter("@BankCode", SqlDbType.NVarChar,200),
                      new SqlParameter("@ParentId", SqlDbType.Int,4)
                                        };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.TitleName;
            parameters[2].Value = model.BillTypeId;
            parameters[3].Value = model.Money;
            parameters[4].Value = model.States;
            parameters[5].Value = model.CompanyId;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.ClassName;
            parameters[8].Value = model.Telephone;
            parameters[9].Value = model.ProcessRemark;
            parameters[10].Value = model.AdminId;
            parameters[11].Value = model.AddBillTime;
            parameters[12].Value = model.ProcessBillTime;
            parameters[13].Value = model.TaxId;
            parameters[14].Value = model.AddressTel;
            parameters[15].Value = model.BankCode;
            parameters[16].Value = model.ParentId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbBill model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbBill set ");
            strSql.Append("TitleName=@TitleName,");
            strSql.Append("BillTypeId=@BillTypeId,");
            strSql.Append("Money=@Money,");
            strSql.Append("States=@States,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("ClassName=@ClassName,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("ProcessRemark=@ProcessRemark,");
            strSql.Append("AdminId=@AdminId,");
            strSql.Append("AddBillTime=@AddBillTime,");
            strSql.Append("ProcessBillTime=@ProcessBillTime,");
            strSql.Append("TaxId=@TaxId,");
            strSql.Append("AddressTel=@AddressTel,");
            strSql.Append("BankCode=@BankCode");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@TitleName", SqlDbType.NVarChar,200),
					new SqlParameter("@BillTypeId", SqlDbType.Int,4),
					new SqlParameter("@Money", SqlDbType.Float,8),
					new SqlParameter("@States", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@ClassName", SqlDbType.NVarChar,50),
                    new SqlParameter("@Telephone", SqlDbType.NVarChar,50),
                    new SqlParameter("@ProcessRemark", SqlDbType.NVarChar,500),
                    new SqlParameter("@AdminId", SqlDbType.Int,4),
                    new SqlParameter("@AddBillTime", SqlDbType.DateTime),
                    new SqlParameter("@ProcessBillTime", SqlDbType.DateTime),
                    new SqlParameter("@TaxId", SqlDbType.NVarChar,200),
                    new SqlParameter("@AddressTel", SqlDbType.NVarChar,200),
                    new SqlParameter("@BankCode", SqlDbType.NVarChar,200),
					new SqlParameter("@Id", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.TitleName;
            parameters[1].Value = model.BillTypeId;
            parameters[2].Value = model.Money;
            parameters[3].Value = model.States;
            parameters[4].Value = model.CompanyId;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.ClassName;
            parameters[7].Value = model.Telephone;
            parameters[8].Value = model.ProcessRemark;
            parameters[9].Value = model.AdminId;
            parameters[10].Value = model.AddBillTime;
            parameters[11].Value = model.ProcessBillTime;
            parameters[12].Value = model.TaxId;
            parameters[13].Value = model.AddressTel;
            parameters[14].Value = model.BankCode;
            parameters[15].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbBill ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,50)			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbBill ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbBill GetModel(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,TitleName,BillTypeId,Money,States,CompanyId,Remark,ClassName,Telephone, ProcessRemark,AdminId,AddBillTime,ProcessBillTime,TaxId,AddressTel,BankCode from tbBill ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,50)			};
            parameters[0].Value = Id;

            Gift.Model.tbBill model = new Gift.Model.tbBill();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id =  ds.Tables[0].Rows[0]["Id"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TitleName"] != null && ds.Tables[0].Rows[0]["TitleName"].ToString() != "")
                {
                    model.TitleName = ds.Tables[0].Rows[0]["TitleName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BillTypeId"] != null && ds.Tables[0].Rows[0]["BillTypeId"].ToString() != "")
                {
                    model.BillTypeId = int.Parse(ds.Tables[0].Rows[0]["BillTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Money"] != null && ds.Tables[0].Rows[0]["Money"].ToString() != "")
                {
                    model.Money = decimal.Parse(ds.Tables[0].Rows[0]["Money"].ToString());
                }
                if (ds.Tables[0].Rows[0]["States"] != null && ds.Tables[0].Rows[0]["States"].ToString() != "")
                {
                    model.States = ds.Tables[0].Rows[0]["States"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyId"] != null && ds.Tables[0].Rows[0]["CompanyId"].ToString() != "")
                {
                    model.CompanyId = int.Parse(ds.Tables[0].Rows[0]["CompanyId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null )
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ClassName"] != null && ds.Tables[0].Rows[0]["ClassName"].ToString() != "")
                {
                    model.ClassName = ds.Tables[0].Rows[0]["ClassName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Telephone"] != null && ds.Tables[0].Rows[0]["Telephone"].ToString() != "")
                {
                    model.Telephone = ds.Tables[0].Rows[0]["Telephone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProcessRemark"] != null && ds.Tables[0].Rows[0]["ProcessRemark"].ToString() != "")
                {
                    model.ProcessRemark = ds.Tables[0].Rows[0]["ProcessRemark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AdminId"] != null && ds.Tables[0].Rows[0]["AdminId"].ToString() != "")
                {
                    model.AdminId = int.Parse(ds.Tables[0].Rows[0]["AdminId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddBillTime"] != null && ds.Tables[0].Rows[0]["AddBillTime"].ToString() != "")
                {
                    model.AddBillTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddBillTime"].ToString());
                }

                if (ds.Tables[0].Rows[0]["ProcessBillTime"] != null && ds.Tables[0].Rows[0]["ProcessBillTime"].ToString() != "")
                {
                    model.ProcessBillTime = DateTime.Parse(ds.Tables[0].Rows[0]["ProcessBillTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxId"] != null && ds.Tables[0].Rows[0]["TaxId"].ToString() != "")
                {
                    model.TaxId = ds.Tables[0].Rows[0]["TaxId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddressTel"] != null && ds.Tables[0].Rows[0]["AddressTel"].ToString() != "")
                {
                    model.AddressTel = ds.Tables[0].Rows[0]["AddressTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BankCode"] != null && ds.Tables[0].Rows[0]["BankCode"].ToString() != "")
                {
                    model.BankCode = ds.Tables[0].Rows[0]["BankCode"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,TitleName,BillTypeId,Money,States,CompanyId,Remark,ClassName,Telephone, ProcessRemark,AdminId,AddBillTime,taxId,AddressTel,BankCode ");
            strSql.Append(" ,(select [name] from tbCompany where id=tbBill.CompanyId) as CompanyName ");
            strSql.Append(", (select [name] from tbBillType where id = tbBill.BillTypeId) as BillTypeName ");
            strSql.Append(", (select [AdminUser] from tbAdminlogin where AdminId = tbBill.AdminId) as AdminUser ");
            strSql.Append(", case when AddBillTime = ProcessBillTime then null else ProcessBillTime end as ProcessBillTime ");
            strSql.Append(" FROM tbBill ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by AddBillTime desc, TitleName ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTypeList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,[name] ");
            strSql.Append(" FROM tbBillType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,TitleName,BillTypeId,Money,States,CompanyId,Remark,ClassName,Telephone, ProcessRemark,AdminId,AddBillTime,ProcessBillTime ");
            strSql.Append(" FROM tbBill ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbBill ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbBill T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  Method
    }
}



